package es.android.lottoscan;

import java.util.Calendar;
import java.util.Date;

import android.util.Log;

public class LottoData {

	static final String TAG = "LottoData";
	
	// El número del Boleto
	String number;
	// La serie
	int serialNumber;
	// La fracción
	int fractionNumber;
	// El número de sorteo: a pertir del número vamos a obtener la fecha
	int dateAsNumber;
	int year;
	int mysteryNumber;
	
	;
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public int getSerialNumber() {
		return serialNumber;
	}
	public void setSerialNumber(int serialNumber) {
		this.serialNumber = serialNumber;
	}
	public int getFractionNumber() {
		return fractionNumber;
	}
	public void setFractionNumber(int fractionNumber) {
		this.fractionNumber = fractionNumber;
	}
	public int getDateAsNumber() {
		return dateAsNumber;
	}
	public void setDateAsNumber(int dateAsNumber) {
		this.dateAsNumber = dateAsNumber;
	}

	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
	public int getMysteryNumber() {
		return mysteryNumber;
	}
	public void setMysteryNumber(int mysteryNumber) {
		this.mysteryNumber = mysteryNumber;
	}
	// Devuelve la fecha convertida
	public Date getDate() {
		Log.d(TAG, "DateAsNumber = " + dateAsNumber);
		Calendar c=Calendar.getInstance();
		int y=c.get(Calendar.YEAR);
		boolean decadeChange=((y%10)<year); //Si el a�o actual es menor al del boleto (ej:a�o=2010 bloeto=2009)
		y/=10;
		y*=10;
		y+=year;
		if (decadeChange)
			y-=10;
		int w=dateAsNumber>>1;
		int dow=7;
		if ((dateAsNumber&1)==1)
			dow=5;
		c.clear();
		c.set(Calendar.YEAR, y);
		if (dateAsNumber == 102) {
			c.set(Calendar.MONTH, 11);
			c.set(Calendar.DAY_OF_MONTH, 22);			
		} else {
			c.set(Calendar.WEEK_OF_YEAR, w);
			c.set(Calendar.DAY_OF_WEEK, dow);			
		}
		
		Log.d(TAG, "DateAsDate = " + c.getTime());
		return c.getTime(); 
	}
	
	// Parsea el valor devuelto por el escánner de código de barras 
	public void parseNumberString(String numberString) {
		if (numberString.length() < 20) return; // Comprobación
		number = numberString.substring(11,16);
		year = Integer.valueOf(numberString.substring(4,5));
		dateAsNumber = Integer.valueOf(numberString.substring(1,4));
		fractionNumber = Integer.valueOf(numberString.substring(5,7));
		serialNumber = Integer.valueOf(numberString.substring(7,10));
		mysteryNumber = Integer.valueOf(numberString.substring(16,20));
		//51020051500 31643 8590
		Log.d(TAG, "Number = " + number);
	}
}
